package com.hxgis.web.controller;

import com.hxgis.common.base.ResultVO;
import com.hxgis.common.parms.RangeParm;
import com.hxgis.common.service.ExtremeProcService;
import com.hxgis.common.service.RainAnalysisService;
import com.hxgis.common.service.RainExtremeInfoService;
import com.hxgis.common.utils.ResultVOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author cqj
 * @version 1.0
 * @date 2019/10/23 9:54
 */
@RestController
@RequestMapping("rain/analysis")
@Slf4j
@Api(tags = "RainAnalysisController")
public class RainAnalysisController {

    @Autowired
    private RainAnalysisService rainAnalysisService;

    @Autowired
    private RainExtremeInfoService extremeInfoService;

    @Autowired
    ExtremeProcService extremeProcService;

    @Value("${save.colorFigPath}")
    private String colorFigPath;


    /**
     * 分级降水 占比与频率统计
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param leftYear 查询范围左
     * @param rightYear 查询范围右
     * @param flag 08 or 20
     * @param type 占比：1 频率：2
     * @param stationNo 站点id
     * @param rangeParms
     * @return
     */
    @ApiOperation(value="分级降水统计",  notes="startTime：2020061100，endTime：2020061106")
    @PostMapping("/range/group")
    public ResultVO rangeGroup(@RequestParam("startTime") String startTime,
                               @RequestParam("endTime") String endTime,
                               @RequestParam(value = "leftYear",required = false) String leftYear,
                               @RequestParam(value = "rightYear",required = false) String rightYear,
                               @RequestParam(value = "flag",required = false) String flag,
                               @RequestParam(value = "type",required = false) String type,
                               @RequestParam(value = "stationNo",required = false) String stationNo,
                               @RequestBody List<RangeParm> rangeParms) {
        return ResultVOUtil.success(rainAnalysisService.rangeGroup(startTime, endTime, leftYear, rightYear ,flag, type, stationNo, rangeParms));
    }



    /**
     *
     * @param startTime
     * @param endTime
     * @param leftTime
     * @param rightTime
     * @param flag 20 08
     * @param type 1:连续降水量 2：连续降水日 3：连续无降水日
     * @param stationNo
     * @param riverName 流域名称
     * @return
     */
    @GetMapping("extreme/proc")
    public ResultVO extremeProc(@RequestParam(value = "startTime",required = false) String startTime,
                                   @RequestParam(value = "endTime",required = false) String endTime,
                                   @RequestParam(value = "leftTime",required = false) String leftTime,
                                   @RequestParam(value = "rightTime",required = false) String rightTime,
                                   @RequestParam(value = "flag",required = false) String flag,
                                   @RequestParam(value = "type",required = false) Integer type,
                                   @RequestParam(value = "stationNo",required = false) String stationNo,
                                   @RequestParam(value = "riverName",required = false) String riverName
    ){
        return extremeProcService.queryExtremProcInfos(startTime, endTime, leftTime, rightTime, flag, type, stationNo, riverName);
    }


    /**
     *
     * @param startTime
     * @param endTime
     * @param leftTime
     * @param rightTime
     * @param flag
     * @param continueDay
     * @param riverName 流域名称
     * @return
     */
    @ApiOperation(value="极端事件 极端日降水接口", notes="startTime：开始时间，endTime：结束时间，查看范围开始时间：leftTime" +
            " 查看范围结束时间：rightTime  flag:20 08 continueDay:持续天数 stationNo：站点id")
    @GetMapping("extreme/day/rain")
    public ResultVO extremeDayRain(@RequestParam(value = "startTime",required = false) String startTime,
                      @RequestParam(value = "endTime",required = false) String endTime,
                      @RequestParam(value = "leftTime",required = false) String leftTime,
                      @RequestParam(value = "rightTime",required = false) String rightTime,
                      @RequestParam(value = "flag",required = false) String flag,
                      @RequestParam(value = "continueDay",required = false) Integer continueDay,
                      @RequestParam(value = "stationNo",required = false) String stationNo,
                      @RequestParam(value = "riverName",required = false) String riverName
                     ){
        return extremeInfoService.queryRailSum(startTime, endTime, leftTime, rightTime, flag, continueDay, stationNo, riverName);
    }





    /**
     * 情报分析降雨日统计接口
     * @param startTime
     * @param endTime
     * @param leftTime 数据范围开始时间
     * @param rightTime 数据范围结束时间
     * @param type
     * @param flag20 gt,xx,lt,xx  gt,1,lt,xx
     * @param flag08 gt,xx,lt,xx
     * @return
     */
    @ApiOperation(value="情报分析降雨日统计接口", notes="startTime：开始时间，endTime：结束时间，查看范围开始时间：leftTime，" +
            "查看范围结束时间：rightTime,type:无雨日，雨日，暴雨日，大暴雨日，自定义，只有当type不为自定义的时候flag20 和 flag08为空， flag20：gt,xx,lt,xx  gt,1,lt,xx，flag08：gt,xx,lt,xx  gt,1,lt,xx，stationNo：可以为空，如果查询所有的信息就不传")
    @GetMapping("intelligence/by/day")
    public ResultVO intelligenceByRainDay(@RequestParam(value = "startTime",required = false) String startTime,
                                        @RequestParam(value = "endTime",required = false) String endTime,
                                        @RequestParam(value = "leftTime",required = false) String leftTime,
                                        @RequestParam(value = "rightTime",required = false) String rightTime,
                                        @RequestParam(value = "type",required = false) String type,
                                        @RequestParam(value = "flag20",required = false) String flag20,
                                        @RequestParam(value = "flag08",required = false) String flag08,
                                        @RequestParam(value = "stationNo",required = false) String stationNo
                                          ){
        return rainAnalysisService.intelligenceByRainDay(startTime, endTime, leftTime, rightTime, type, flag20, flag08, stationNo);
    }

    /**
     * 下载情报分析降雨日色斑图
     * @param startTime
     * @param endTime
     * @param leftTime
     * @param rightTime
     * @param type
     * @param mark now max min
     * @param sign BAOYU QH_PRE_DAYDISTANCE
     * @param flag20
     * @param flag08
     */
    @GetMapping("down/intelligence/by/day")
    public void downIntelligenceByRainDay(@RequestParam(value = "startTime",required = false) String startTime,
                                          @RequestParam(value = "endTime",required = false) String endTime,
                                          @RequestParam(value = "leftTime",required = false) String leftTime,
                                          @RequestParam(value = "rightTime",required = false) String rightTime,
                                          @RequestParam(value = "type",required = false) String type,
                                          @RequestParam(value = "mark",required = false) String mark,
                                          @RequestParam(value = "flag20",required = false) String flag20,
                                          @RequestParam(value = "flag08",required = false) String flag08,
                                          @RequestParam(value = "sign",required = false) String sign,
                                          @RequestParam(value = "isColreFig",required = false) boolean isColreFig,
                                          HttpServletResponse response
    ){
//        RainInfoAnalysisVO rainInfoAnalysisVO  = (RainInfoAnalysisVO) rainAnalysisService.intelligenceByRainDay(startTime, endTime, leftTime, rightTime, type, flag20, flag08, null).getData();
//
////        ExcelUtils.exportExcel(rainInfoAnalysisVO.getSingStationInfos(),startTime + "-" + endTime +"全国累计降雨日 " + flag + "色斑图数据","sheet1", RainInfoAnaStationDTO.class,startTime + "-" + endTime +"全国累计降雨日 " + flag + "色斑图数据" + ".xls",response);
//
//        //todo 生成色斑图测试后面写方法生成
//        String url = null;
//        try {
//            //单位
//            String unit = "日";
//            String urlName = null;
//            String firstTitle = null;
//            String secondTitle = null;
//
//            if (Objects.equals(sign,"jp")){
//                urlName = "全国累计降雨日" + type + " 历史距平" +
//                          DataColorFigEnum.getSplashColorEnum(mark).getMessage();
//            }else{
//                urlName = "全国累计降雨日 " + type + DataColorFigEnum.getSplashColorEnum(mark).getMessage();
//            }
//
//
//            if (Objects.equals(startTime,endTime)){
//                secondTitle = TimeUtil.changeStyle(startTime,FORMATOR_YMD,ZN_FORMATOR_YMD);
//            }else{
//                secondTitle = TimeUtil.changeStyle(startTime,FORMATOR_YMD,ZN_FORMATOR_YMD) + "到"+ TimeUtil.changeStyle(endTime,FORMATOR_YMD,ZN_FORMATOR_YMD);
//            }
//
//            firstTitle = urlName + "[" + unit + "]";
//
//            //准备数据
//            List<Double> values = new ArrayList<>();
//            List<RainInfoAnaStationDTO> singStationInfos = rainInfoAnalysisVO.getSingStationInfos();
//            if (Objects.equals(sign,"BAOYU")){
//                if (Objects.equals(mark, DataColorFigEnum.NOW.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getValues() < 999999)).collect(Collectors.toList());
//                }else if (Objects.equals(mark, DataColorFigEnum.MAX.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getMaxValues() < 999999)).collect(Collectors.toList());
//                } else if (Objects.equals(mark, DataColorFigEnum.MIN.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getMinValues() < 999999)).collect(Collectors.toList());
//                }else if (Objects.equals(mark, DataColorFigEnum.AVG.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getAvg30() < 999999)).collect(Collectors.toList());
//                }
//            }else{
//                if (Objects.equals(mark, DataColorFigEnum.NOW.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getHistoryAvgRate() < 999999)).collect(Collectors.toList());
//                }else if (Objects.equals(mark, DataColorFigEnum.MAX.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getMaxHistoryAvgRate() < 999999)).collect(Collectors.toList());
//                } else if (Objects.equals(mark, DataColorFigEnum.MIN.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getMinHistoryAvgRate() < 999999)).collect(Collectors.toList());
//                }
//            }
//
//
//            if (Objects.equals(sign,"QH_PRE_DAYDISTANCE")){
//                if (Objects.equals(mark, DataColorFigEnum.NOW.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getValues()).collect(Collectors.toList());
//                }else if (Objects.equals(mark, DataColorFigEnum.MAX.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getMaxValues()).collect(Collectors.toList());
//                } else if (Objects.equals(mark, DataColorFigEnum.MIN.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getMinValues()).collect(Collectors.toList());
//                }else if (Objects.equals(mark, DataColorFigEnum.AVG.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getAvg30()).collect(Collectors.toList());
//                }
//            }else{
//                if (Objects.equals(mark, DataColorFigEnum.NOW.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getHistoryAvgRate()).collect(Collectors.toList());
//                }else if (Objects.equals(mark, DataColorFigEnum.MAX.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getMaxHistoryAvgRate()).collect(Collectors.toList());
//                } else if (Objects.equals(mark, DataColorFigEnum.MIN.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getMinHistoryAvgRate()).collect(Collectors.toList());
//                }
//            }
//            List<Double> latLists = singStationInfos.stream().map(u -> u.getLat()).collect(Collectors.toList());
//            List<Double> lonLists = singStationInfos.stream().map(u -> u.getLon()).collect(Collectors.toList());
//            url = rainStormGraph.genColorMap(colorFigPath + File.separator + urlName + secondTitle +  "色斑图" + ".png",
//                    firstTitle, secondTitle, isColreFig,  unit, 999999d , sign,
//                    values, latLists, lonLists);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        File file = new File(url);
//        if (file.exists()) {
//            DownFileUtil.printFile(file, response);
//            file.delete();
//        }

    }

    /**
     * 情报分析降雨量统计接口
     * @param startTime
     * @param endTime
     * @param leftTime 数据范围开始时间
     * @param rightTime 数据范围结束时间
     * @param flag
     * @return
     */
    @ApiOperation(value="情报分析降雨量统计接口", notes="startTime：开始时间，endTime：结束时间，查看范围开始时间：leftTime，" +
            "查看范围结束时间：rightTime,flag:20或者08，stationNo：可以为空，如果查询所有的信息就不传")
    @GetMapping("intelligence")
    public ResultVO intelligence(@RequestParam(value = "startTime",required = false) String startTime,
                                  @RequestParam(value = "endTime",required = false) String endTime,
                                 @RequestParam(value = "leftTime",required = false) String leftTime,
                                 @RequestParam(value = "rightTime",required = false) String rightTime,
                                  @RequestParam(value = "flag",required = false) String flag,
                                 @RequestParam(value = "stationNo",required = false) String stationNo){

        return rainAnalysisService.intelligence(startTime, endTime, leftTime, rightTime, flag, stationNo);
    }


    /**
     * 下载色斑图
     * @param startTime
     * @param endTime
     * @param leftTime
     * @param rightTime
     * @param isColreFig true :色斑图 false:散点图
     * @param flag 20 08
     * @param sign QH_PRE_ANOMALY  QH_PRE
     * @param type now
     * @param response
     */
    @GetMapping("down/intelligence")
    public void downIntelligence(@RequestParam(value = "startTime",required = false) String startTime,
                                 @RequestParam(value = "endTime",required = false) String endTime,
                                 @RequestParam(value = "leftTime",required = false) String leftTime,
                                 @RequestParam(value = "rightTime",required = false) String rightTime,
                                 @RequestParam(value = "flag",required = false) String flag,
                                 @RequestParam(value = "isColreFig",required = false) boolean isColreFig,
                                 @RequestParam(value = "sign",required = false) String sign,
                                 @RequestParam(value = "type",required = false) String type,
                                     HttpServletResponse response){

//        RainInfoAnalysisVO rainInfoAnalysisVO  = (RainInfoAnalysisVO) rainAnalysisService.intelligence(startTime, endTime, leftTime, rightTime, flag, null).getData();
//
//
//        //todo 写入excel
////        ExcelUtils.exportExcel(rainInfoAnalysisVO.getSingStationInfos(),startTime + "-" + endTime +"日 " + flag + "色斑图数据","sheet1", RainInfoAnaStationDTO.class,startTime + "-" + endTime +"日 " + flag + "色斑图数据" + ".xls",response);
////        ExcelUtils.exportExcel(rainInfoAnalysisVO.getYearDataValuesDTOS(),startTime + "-" + endTime +"日 " + flag + "历年总降水量","sheet1", YearDataValuesDTO.class,startTime + "-" + endTime +"日 " + flag + "历年总降水量" + ".xls",response);
//
//
//        //todo 生成色斑图测试后面写方法生成
//        String url = null;
//        try {
//            //单位
//            String unit = null;
//            String firstTitle = null;
//            String secondTitle = null;
//
//            String urlName = "全国累计降水量(" + flag + "-" + flag +")" +
//                    SplashColorEnum.getSplashColorEnum(sign).getMessage() + DataColorFigEnum.getSplashColorEnum(type).getMessage();
//
//            if (Objects.equals(startTime,endTime)){
//                secondTitle = TimeUtil.changeStyle(startTime,FORMATOR_YMD,ZN_FORMATOR_YMD);
//            }else{
//                secondTitle = TimeUtil.changeStyle(startTime,FORMATOR_YMD,ZN_FORMATOR_YMD) + "到"+ TimeUtil.changeStyle(endTime,FORMATOR_YMD,ZN_FORMATOR_YMD);
//            }
//            if (Objects.equals(sign,SplashColorEnum.QH_PRE_DAYDISTANCE.getFlag())){
//                unit = "日";
//            }else  if (Objects.equals(sign,SplashColorEnum.QH_PRE_ANOMALY.getFlag())){
//                unit = "%";
//            }else {
//                unit = "mm";
//            }
//            firstTitle = urlName + "[" + unit + "]";
//
//            //准备数据
//            List<Double> values = new ArrayList<>();
//            List<RainInfoAnaStationDTO> singStationInfos = rainInfoAnalysisVO.getSingStationInfos();
//
//            if (Objects.equals(sign,"QH_PRE")){
//                if (Objects.equals(type, DataColorFigEnum.NOW.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getValues() < 999999)).collect(Collectors.toList());
//                }else if (Objects.equals(type, DataColorFigEnum.MAX.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getMaxValues() < 999999)).collect(Collectors.toList());
//                } else if (Objects.equals(type, DataColorFigEnum.MIN.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getMinValues() < 999999)).collect(Collectors.toList());
//                }else if (Objects.equals(type, DataColorFigEnum.AVG.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getAvg30() < 999999)).collect(Collectors.toList());
//                }
//            }else{
//                if (Objects.equals(type, DataColorFigEnum.NOW.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getHistoryAvgRate() < 999999)).collect(Collectors.toList());
//                }else if (Objects.equals(type, DataColorFigEnum.MAX.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getMaxHistoryAvgRate() < 999999)).collect(Collectors.toList());
//                } else if (Objects.equals(type, DataColorFigEnum.MIN.getType())){
//                    singStationInfos = singStationInfos.stream().filter((p) -> (p.getMinHistoryAvgRate() < 999999)).collect(Collectors.toList());
//                }
//            }
//
//
//
//            if (Objects.equals(sign,"QH_PRE")){
//                if (Objects.equals(type, DataColorFigEnum.NOW.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getValues()).collect(Collectors.toList());
//                }else if (Objects.equals(type, DataColorFigEnum.MAX.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getMaxValues()).collect(Collectors.toList());
//                } else if (Objects.equals(type, DataColorFigEnum.MIN.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getMinValues()).collect(Collectors.toList());
//                }else if (Objects.equals(type, DataColorFigEnum.AVG.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getAvg30()).collect(Collectors.toList());
//                }
//            }else{
//                if (Objects.equals(type, DataColorFigEnum.NOW.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getHistoryAvgRate()).collect(Collectors.toList());
//                }else if (Objects.equals(type, DataColorFigEnum.MAX.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getMaxHistoryAvgRate()).collect(Collectors.toList());
//                } else if (Objects.equals(type, DataColorFigEnum.MIN.getType())){
//                    values  = singStationInfos.stream().map(u -> u.getMinHistoryAvgRate()).collect(Collectors.toList());
//                }
//            }
//            List<Double> latLists = singStationInfos.stream().map(u -> u.getLat()).collect(Collectors.toList());
//            List<Double> lonLists = singStationInfos.stream().map(u -> u.getLon()).collect(Collectors.toList());
//            url = rainStormGraph.genColorMap(colorFigPath + File.separator + urlName + secondTitle +  "色斑图" + ".png",
//            firstTitle, secondTitle, isColreFig,  unit, 999999d, SplashColorEnum.getSplashColorEnum(sign).getFlag(),
//                    values, latLists, lonLists);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        File file = new File(url);
//        if (file.exists()) {
//            DownFileUtil.printFile(file, response);
//            file.delete();
//        }


    }


    /**
     * 极值排位单日多日累计降水量
     * @param startTime
     * @param endTime
     * @param leftTime 数据范围开始时间
     * @param rightTime 数据范围结束时间
     * @param flag 20 08
     * @param type 类型 旬排位 月排位 季排位 历史排位 自定义排位
     * @param stationNo
     * @return
     */
    @ApiOperation(value="极值排位单日多日累计降水量", notes="startTime：开始时间，endTime：结束时间，rankStartTime：自定义开始时间，rankEndTime 自定义结束时间，查看范围开始时间：leftTime，" +
            "查看范围结束时间：rightTime,flag:20或者08，" +
            "type 类型 旬排位 月排位 季排位 历史排位 自定义排位，stationNo：可以为空，如果查询所有的信息就不传")
    @GetMapping("extreme/sum")
    public ResultVO extremeSumRain(@RequestParam(value = "startTime",required = false) String startTime,
                                  @RequestParam(value = "endTime",required = false) String endTime,
                                   @RequestParam(value = "continueDay",required = false) Integer continueDay,
                                  @RequestParam(value = "leftTime",required = false) String leftTime,
                                   @RequestParam(value = "rightTime",required = false) String rightTime,
                                   @RequestParam(value = "flag",required = false) String flag,
                                  @RequestParam(value = "type",required = false) String type,
                                   @RequestParam(value = "stationNo",required = false) String stationNo){

        return rainAnalysisService.extremeSumRain(startTime, endTime, continueDay, leftTime, rightTime, flag, type, stationNo);
    }

    /**
     * 极值排位降水过程接口
     * @param startTime
     * @param endTime
     * @param leftTime 数据范围开始时间
     * @param rightTime 数据范围结束时间
     * @param flag 20 08
     * @param type 类型 旬排位 月排位 季排位 历史排位 自定义排位
     * @param elementType 1:连续降水过程  2：连续无降水过程
     * @param sign 标识 sum  day
     * @param stationNo
     * @return
     */
    @ApiOperation(value="极值排位降水过程接口", notes="startTime：开始时间，endTime：结束时间（自定义排位才会出endTime），查看范围开始时间：leftTime，" +
            "查看范围结束时间：rightTime,flag:20或者08，" +
            "type 类型 旬排位 月排位 季排位 历史排位 自定义排位，elementType 1:连续降水过程  2：连续无降水过程，sign 标识 sum（降水量）  day（持续日数），stationNo：可以为空，如果查询所有的信息就不传")
    @GetMapping("/contunue")
    public ResultVO contunueRain(@RequestParam(value = "startTime",required = false) String startTime,
                                   @RequestParam(value = "endTime",required = false) String endTime,
                                   @RequestParam(value = "leftTime",required = false) String leftTime,
                                   @RequestParam(value = "rightTime",required = false) String rightTime,
                                   @RequestParam(value = "flag",required = false) String flag,
                                   @RequestParam(value = "type",required = false) String type,
                                   @RequestParam(value = "elementType",required = false) String elementType,
                                   @RequestParam(value = "sign",required = false) String sign,
                                   @RequestParam(value = "stationNo",required = false) String stationNo){

        return rainAnalysisService.contunueRain(startTime, endTime, leftTime, rightTime, flag, type, elementType, sign, stationNo);
    }


}
